package com.dnzx.security;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import com.dnzx.client.IMenuClient;
import com.dnzx.model.system.MenuBO;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>配置URL+权限管理器</p>
 * @Package com.cbs.filter.security
 * @author Steven
 * 
 * @date 2017/9/27 14:12
 * @version V1.0
 */
public class MyFilterInvocationSecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {

	private static Logger logger = LoggerFactory
			.getLogger(MyFilterInvocationSecurityMetadataSource.class);
	private final Map<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();
	@Autowired
	private IMenuClient cmsResourceClient;

	@Override
	public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
		final HttpServletRequest request = ((FilterInvocation) object).getRequest();
		for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {
			if (entry.getKey().matches(request)) {
				return entry.getValue();
			}
		}
		return null;
	}

	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
		for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {
			configAttributes.addAll(entry.getValue());
		}
		return configAttributes;
	}

	@Override
	public boolean supports(Class<?> clazz) {
		return true;
	}

	/**
	 * 初始化加载数据
	 */
	@PostConstruct
	public void loadRequestMap() {
//		logger.info("数据库资源开始加载......");
//		// 获取资源列表
//		List<MenuBO> resources = cmsResourceClient.findAll();
//		// 遍历资源
//		for (MenuBO resource : resources) {
//			// 判断url不能为空
//			if (StringUtils.isBlank(resource.getUrl())) {
//				continue;
//			}
//			// 设置url路径
//			AntPathRequestMatcher antPathRequestMatcher = new AntPathRequestMatcher(
//					resource.getUrl());
//			// 判断是存在
//			if (requestMap.containsKey(antPathRequestMatcher)) {
////				SecurityConfig sc = new SecurityConfig(resource.getCode());
//				Collection<ConfigAttribute> collect = requestMap.get(antPathRequestMatcher);
////				collect.add(sc);
//			} else {
//				// 如果不存在
//				Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
////				SecurityConfig sc = new SecurityConfig(resource.getCode());
////				configAttributes.add(sc);
//				requestMap.put(antPathRequestMatcher, configAttributes);
//			}
//		}
	}
}
